package com.blb.zy_community.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.zy_community.entity.ResponseResult;
import com.blb.zy_community.entity.ZyOwner;
import com.blb.zy_community.entity.ZyOwnerRoom;
import com.blb.zy_community.entity.ZyOwnerRoomRecord;
import com.blb.zy_community.mapper.ZyOwnerMapper;
import com.blb.zy_community.mapper.ZyOwnerRoomMapper;
import com.blb.zy_community.service.IZyOwnerRoomRecordService;
import com.blb.zy_community.service.IZyOwnerRoomService;
import com.blb.zy_community.utils.JwtUtils;
import com.blb.zy_community.utils.RsaUtils;
import com.blb.zy_community.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 房屋绑定表  前端控制器
 * </p>
 *
 * @author 二组
 * @since 2023-06-18
 */
@RestController
@CrossOrigin
public class ZyOwnerRoomController {
    @Autowired
    private IZyOwnerRoomService zyOwnerRoomService;
    @Autowired
    private IZyOwnerRoomRecordService zyOwnerRoomRecordService;

    @Autowired
    private ZyOwnerMapper zyOwnerMapper;

    @GetMapping("zyOwnerRoom-page")
    public ResponseResult<Page<ZyOwnerRoom>> getZyOwnerRoomPage(@RequestParam(required = false, defaultValue = "1") Long current,
                                                                @RequestParam(required = false, defaultValue = "5") Long size,
                                                                @RequestParam(required = false) String roomStatus,
                                                                @RequestParam(required = false) String communityId) {
        Page<ZyOwnerRoom> page = zyOwnerRoomService.selectZyOwnerRoom(new Page<>(current, size), roomStatus , communityId);
        return ResponseResult.ok(page);
    }

    @GetMapping("zyOwnerRoomList")
    public ResponseResult<String> getZyOwnerRoomList(@RequestParam(required = false) Long id) {
        List<ZyOwnerRoomRecord> list = zyOwnerRoomRecordService.selectZyOwnerRoomRecord(id);
        return ResponseResult.ok(list);
    }

    /**
     * 根据ownerRoomId(主键)删除用户房间表
     * @param ownerRoomId
     * @return
     */
    @DeleteMapping("/delete/{ownerRoomId}")
    public ResponseResult<Long> deleteByOwnerRoomId(@PathVariable("ownerRoomId") Long ownerRoomId) {
        zyOwnerRoomService.removeById(ownerRoomId);
        return ResponseResult.ok(ownerRoomId);
    }

    /**
     *  添加业主审核
     */
    @PostMapping("ownerRoom")
    public ResponseResult<String> addOwnerRoom(@RequestBody ZyOwnerRoom zyOwnerRoom, HttpServletRequest request){
        zyOwnerRoom.setRoomStatus("Auditing");
        String token = request.getHeader("Authorization");
        String createUser = JwtUtils.getUsernameFromToken(token, RsaUtils.publicKey);
        ZyOwner ownerRealName = zyOwnerMapper.selectOne(new QueryWrapper<ZyOwner>().eq("owner_real_name", createUser));
        zyOwnerRoom.setOwnerId(Long.valueOf(String.valueOf(ownerRealName.getOwnerId())));
        String str = String.valueOf(new Long((long) ((Math.random() + 1) * Math.pow(10, 19 - 1))).longValue());
        zyOwnerRoom.setOwnerRoomId(Long.valueOf(str));
        zyOwnerRoom.setCreateTime(LocalDateTime.now());
        zyOwnerRoomService.save(zyOwnerRoom);
        return ResponseResult.ok(zyOwnerRoom);
    }


    @GetMapping("wx-owner-room")
    public ResponseResult<ZyOwnerRoom> wxSelectOwnerRoom(HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String createUser = JwtUtils.getUsernameFromToken(token, RsaUtils.publicKey);
        ZyOwner ownerRealName = zyOwnerMapper.selectOne(new QueryWrapper<ZyOwner>().eq("owner_real_name", createUser));
        List<ZyOwnerRoom> zyOwnerRooms = zyOwnerRoomService.selectByOwnerId(ownerRealName.getOwnerId());
        return ResponseResult.ok(zyOwnerRooms);
    }

}
